package com.superyee.commonlib.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.superyee.commonlib.R;
import com.superyee.commonlib.constants.StaticConstant;
import com.superyee.commonlib.widgets.ActionBar;

/**
 * 版权所有 金泰节能科技有限公司,保留所有版权。<br>
 * 项目名：	农信 - Android客户端<br>
 * 项目描述：提供fragment容器,用于打开单独的fragment。
 * 作者： jingchao 日期：2018-05-22
 */
public class ShowDetailActivity extends BaseActivity {

    public static final int FRAGMENT_ADD = 1;
    public static final int FRAGMENT_REPLACE = 2;
    public String fragmentName, title;
    private Bundle mData;

    public static void showDetail(Context context, Class clas, String title, Bundle data) {
        Intent intent = new Intent(context, ShowDetailActivity.class);
        intent.putExtra("fragmentName", clas.getName());
        intent.putExtra("title", title);
        if (data != null) {
            intent.putExtra(StaticConstant.Extra.DATA, data);
        }
        context.startActivity(intent);
    }

    public static void showDetail(Context context, Class clas, String title) {
        showDetail(context, clas, title, null);
    }


    @Override
    protected int getLayoutId() {
        return R.layout.comm_activity_show_detail;
    }

    @Override
    protected void initPerData(Bundle savedInstanceState) {
        Intent intent = getIntent();
        if (intent != null) {
            Bundle bundle = intent.getExtras();
            fragmentName = bundle.getString("fragmentName");
            title = bundle.getString("title");
            mData = bundle.getBundle(StaticConstant.Extra.DATA);
        }
    }

    @Override
    protected void initData() {

    }


    @Override
    public void onReceiverNotify(int action, Object object, int stateCode) {

    }

    public void initView() {
        if (!TextUtils.isEmpty(title)) {
            ActionBar actionBar = new ActionBar(this);
            ViewGroup rootLayout = findViewById(R.id.rootlayout);
            rootLayout.addView(actionBar, 0);
            actionBar.setTitle(title);
            actionBar.showBack(this);
        }
        addFragmentToStack(fragmentName, FRAGMENT_ADD);
    }


    @Override
    protected void addListener() {

    }

    @Override
    protected void removeListener() {

    }

    private void addFragmentToStack(String fragmentName, int type) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        Fragment newFragment = null;
        try {
            newFragment = (Fragment) Class.forName(fragmentName).newInstance();
            newFragment.setArguments(mData);
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }
        switch (type) {
            case FRAGMENT_ADD:
                ft.add(R.id.container, newFragment, fragmentName);
                break;
            case FRAGMENT_REPLACE:
                ft.replace(R.id.container, newFragment, fragmentName);
                ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                ft.addToBackStack(null);
                break;
        }
        ft.commit();
    }

}
